package com.itheima.edu.info.manager.controller;

import com.itheima.edu.info.manager.domain.Student;
import com.itheima.edu.info.manager.service.StudentService;

import java.util.Scanner;

public class StudentController {
    private StudentService studentService = new StudentService();
    Scanner sc = new Scanner(System.in);

    public void start() {
        studentloop:
        while(true){
            Scanner sc = new Scanner(System.in);
            System.out.println("--------欢迎来到 <学生> 信息管理系统--------");
            System.out.println("请输入您的选择: 1. 添加学生  2. 删除学生  3. 修改学生 4. 查看学生 5. 退出");
            String choice = sc.next();
            switch(choice){
                case "1":
                    // System.out.println("添加学生");
                    addStudent();
                    break;
                case "2":
                    // System.out.println("删除学生");
                    deleteStudentById();
                    break;
                case "3":
                    // System.out.println("修改学生");
                    updateStudent();
                    break;
                case "4":
                    //System.out.println("查看学生");
                    findAllStudent();
                    break;
                case "5":
                    System.out.println("欢迎使用学生管理系统！\n");
                    break studentloop;
                default:
                    System.out.println("输入有误，请重新选择：\n");
                    break;
            }
        }

    }
    // 修改
    public void updateStudent() {
        Student[] stus = studentService.findAllStudent();
        if(stus == null){
            System.out.println("查无信息，请添加后重试\n");
            return;
        }
        System.out.println("请输入你要修改的学生id");
        String updId = sc.next();
        boolean boo = studentService.isExists(updId);
        if(!boo){
            System.out.println("您输入的学生id不存在");
            return;
        }

        Student stu = inputStudentIfo(updId);

        studentService.updateStudent(updId,stu);
        System.out.println("修改成功");
    }

    // 删除
    public void deleteStudentById() {
        Student[] stus = studentService.findAllStudent();
        if(stus == null){
            System.out.println("查无信息，请添加后重试\n");
            return;
        }
            // 1. 键盘录入要删除的学生id
            System.out.println("请输入你要删除的学生id");
            String delId = sc.next();
            // 2. 判断id在容器中是否存在
            boolean boo = studentService.isExists(delId);
            if(!boo){
                System.out.println("您输入的学生id不存在");
                return;
            }
            // 3.调用业务员中的deleteStudentById根据id，删除学生
            studentService.deleteStudentById(delId);
        System.out.println("删除成功!");

    }


    // 添加
    public void addStudent() {
        // StudentService studentService = new StudentService();
        // 1. 键盘接收学生信息

        String id;
        while(true){
            // 2. 将学生信息封装为学生对象
            System.out.println("请输入学生学号");
            id = sc.next();
            boolean flag = studentService.isExists(id);
            if(flag){
                System.out.println("学好已被占用，请重新输入");
            }else{
                break;
            }
        }
        Student stu = inputStudentIfo(id);

        // 3. 将学生对象，传递给StudentService(业务员)中的addStudent方法
        boolean result =  studentService.addStudent(stu);

        // 4. 根据返回的boolea类型结果，在控制台打印成功\失败
        if(result){
            System.out.println("添加成功");
            return;
        }else{
            System.out.println("添加失败");
        }
    }

    // 查询
    public void findAllStudent() {
        Student[] stus = studentService.findAllStudent();
        if(stus == null){
            System.out.println("查无信息，请添加后重试\n");
            return;
        }
        System.out.println("学号\t\t姓名\t年龄\t生日");
        for (int i = 0; i < stus.length; i++) {
            Student stu = stus[i];
            if(stu != null){
                System.out.println( stu.getId() + "\t" +
                                    stu.getName() + "\t" +
                                    stu.getAge() + "\t\t" +
                                    stu.getBirthday());
            }
        }
        System.out.println();
    }

    // 键盘录入学生信息
    public Student inputStudentIfo(String id){
        System.out.println("请输入学生姓名");
        String name = sc.next();
        System.out.println("请输入学生年龄");
        int age = sc.nextInt();
        System.out.println("请输入学生生日");
        String birthday = sc.next();
        Student stu = new Student();
        stu.setId(id);
        stu.setName(name);
        stu.setAge(age);
        stu.setBirthday(birthday);
        return stu;
    }
}
